package com.qs.home.ui.fragment;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.databinding.Observable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.qs.base.base.BaseMyFragment;
import com.qs.base.router.RouterFragmentPath;
import com.qs.base.utils.BaseTimeUtils;
import com.qs.base.utils.FileUtil;
import com.qs.home.BR;
import com.qs.home.R;
import com.qs.home.databinding.HomeFragmentBankResultsBinding;
import com.qs.home.databinding.HomeFragmentIdCardIdentifyResultsBinding;
import com.qs.home.entity.BackResultEntity;
import com.qs.home.entity.IdCardFrontResultEntity;
import com.qs.home.ui.VM.BankCardResultsViewModel;
import com.qs.home.ui.VM.IdentifyDetailsViewModel;
import com.qs.widget.widget.CommonPopupWindow;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

/**
 * 银行卡识别结果信息
 *
 * @Author ltzz
 * @Date 2019/8/22
 */
@Route(path = RouterFragmentPath.Home.BANK_RESULTS_FRAGMENT)
public class BankResultsFragment extends BaseMyFragment<HomeFragmentBankResultsBinding, BankCardResultsViewModel> implements View.OnClickListener {

    @Autowired(name = "bankResult")
    BackResultEntity bankResult;

    @Override
    protected View titleLayout() {
        return null;
    }

    @Override
    protected boolean statusBarDarkFont() {
        return false;
    }

    @Override
    public int initContentView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return R.layout.home_fragment_bank_results;
    }

    @Override
    public int initVariableId() {
        return BR.viewModel;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if (bankResult != null)
            viewModel.bankResult.set(bankResult);
    }

    @Override
    public void initViewObservable() {
        super.initViewObservable();

        //复制
        viewModel.uc.copyTextUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                //获取剪贴板管理器：
                ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
                // 创建普通字符型ClipData
                ClipData mClipData = ClipData.newPlainText("Label", viewModel.uc.copyTextUc.get());
                // 将ClipData内容放到系统剪贴板里。
                cm.setPrimaryClip(mClipData);
                ToastUtils.showShort("文本已复制");
            }
        });
        //导出
        viewModel.uc.exportUc.addOnPropertyChangedCallback(new Observable.OnPropertyChangedCallback() {
            @Override
            public void onPropertyChanged(Observable sender, int propertyId) {
                if (exportPop != null) {
                    exportPop.dismiss();
                }
                initExportPop();
            }
        });
    }

    CommonPopupWindow exportPop;

    //导出弹窗
    private void initExportPop() {
        View photoView = LayoutInflater.from(getActivity()).inflate(R.layout.home_diglog_translate_export, null);
        photoView.findViewById(R.id.txt).setOnClickListener(this);//拍照
        photoView.findViewById(R.id.word).setOnClickListener(this);//相册中选择
        exportPop = new CommonPopupWindow.Builder(getActivity())
                //设置PopupWindow布局
                .setView(photoView)
                //设置宽高
                .setWidthAndHeight(ViewGroup.LayoutParams.MATCH_PARENT,
                        ViewGroup.LayoutParams.WRAP_CONTENT)
                //设置动画
//                .setAnimationStyle(R.style.base_AnimDown)
                //设置背景颜色，取值范围0.0f-1.0f 值越小越暗 1.0f为透明
                .setBackGroundLevel(0.3f)
                //设置PopupWindow里的子View及点击事件
                .setViewOnclickListener(new CommonPopupWindow.ViewInterface() {
                    @Override
                    public void getChildView(View view, int layoutResId) {
                    }
                })
                //设置外部是否可点击 默认是true
                .setOutsideTouchable(true)
                //开始构建
                .create();
        exportPop.showAsDropDown(binding.botView, 0, -(exportPop.getHeight() + binding.botView.getMeasuredHeight()));
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.txt) {
            //导出为txt
            exportPop.dismiss();
            List<String> text = new ArrayList<>();
            text.add("银行：");
            text.add(viewModel.bankResult.get().getBankName() == null ? "" : viewModel.bankResult.get().getBankName());
            text.add("银行卡类型：");
            text.add(viewModel.bankResult.get().getJsonRes().getResult().getBank_card_type() == null ? "" : viewModel.bankResult.get().getJsonRes().getResult().getBank_card_type());
            text.add("卡号：");
            text.add(viewModel.bankResult.get().getBankCardNumber() == null ? "" : viewModel.bankResult.get().getBankCardNumber());
            text.add("有效期（如果有）");
            text.add(viewModel.bankResult.get().getJsonRes().getResult().getValid_date() == null ? "" : viewModel.bankResult.get().getJsonRes().getResult().getValid_date());
            FileUtil.outText(text, "out", BaseTimeUtils.getDeviceTimeOfTimestamp() + "银行卡识别");

        }
        if (id == R.id.word) {
            //导出为word
            exportPop.dismiss();
            List<String> text = new ArrayList<>();
            text.add("银行：");
            text.add(viewModel.bankResult.get().getBankName() == null ? "" : viewModel.bankResult.get().getBankName());
            text.add("银行卡类型：");
            text.add(viewModel.bankResult.get().getJsonRes().getResult().getBank_card_type() == null ? "" : viewModel.bankResult.get().getJsonRes().getResult().getBank_card_type());
            text.add("卡号：");
            text.add(viewModel.bankResult.get().getBankCardNumber() == null ? "" : viewModel.bankResult.get().getBankCardNumber());
            text.add("有效期（如果有）");
            text.add(viewModel.bankResult.get().getJsonRes().getResult().getValid_date() == null ? "" : viewModel.bankResult.get().getJsonRes().getResult().getValid_date());
            FileUtil.outWord(text, "out", BaseTimeUtils.getDeviceTimeOfTimestamp() + "银行卡识别");
        }
    }
}
